package 每日一题;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;

public class 小根堆 {


    public static void main(String[] args) {
        //
        //String []words={"glarko","zlfiwwb","nsfspyox","pwqvwmlgri","qggx","qrkgmliewc","zskaqzwo","zskaqzwo","ijy","htpvnmozay","jqrlad","ccjel","qrkgmliewc","qkjzgws","fqizrrnmif","jqrlad","nbuorw","qrkgmliewc","htpvnmozay","nftk","glarko","hdemkfr","axyak","hdemkfr","nsfspyox","nsfspyox","qrkgmliewc","nftk","nftk","ccjel","qrkgmliewc","ocgjsu","ijy","glarko","nbuorw","nsfspyox","qkjzgws","qkjzgws","fqizrrnmif","pwqvwmlgri","nftk","qrkgmliewc","jqrlad","nftk","zskaqzwo","glarko","nsfspyox","zlfiwwb","hwlvqgkdbo","htpvnmozay","nsfspyox","zskaqzwo","htpvnmozay","zskaqzwo","nbuorw","qkjzgws","zlfiwwb","pwqvwmlgri","zskaqzwo","qengse","glarko","qkjzgws","pwqvwmlgri","fqizrrnmif","nbuorw","nftk","ijy","hdemkfr","nftk","qkjzgws","jqrlad","nftk","ccjel","qggx","ijy","qengse","nftk","htpvnmozay","qengse","eonrg","qengse","fqizrrnmif","hwlvqgkdbo","qengse","qengse","qggx","qkjzgws","qggx","pwqvwmlgri","htpvnmozay","qrkgmliewc","qengse","fqizrrnmif","qkjzgws","qengse","nftk","htpvnmozay","qggx","zlfiwwb","bwp","ocgjsu","qrkgmliewc","ccjel","hdemkfr","nsfspyox","hdemkfr","qggx","zlfiwwb","nsfspyox","ijy","qkjzgws","fqizrrnmif","qkjzgws","qrkgmliewc","glarko","hdemkfr","pwqvwmlgri"};
        //List<String> list = topKFrequent(words, 14);
        //list.stream().forEach(System.out::println);
        //
        PriorityQueue queue=new PriorityQueue();
        int c=5;
        while (c>0){
            queue.offer(c);
            c--;
        }

        queue.stream().forEach(System.out::println);
        System.out.println("----");
        while (queue.size()>0){
            System.out.println(queue.poll());
        }
    }


    public static List<String> topKFrequent(String[] words, int k) {
        List<String> resList=new ArrayList<>();
        HashMap<String,Integer> map=new HashMap<>();
        for (String w:words) {
            map.put(w,map.getOrDefault(w,0)+1);
        }

        PriorityQueue<String> priorityQueue = new PriorityQueue(Comparator.comparingInt(map::get));
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            if(priorityQueue.size()>k){
                priorityQueue.poll();
            }else{
                priorityQueue.add(iterator.next().getKey());
            }
        }
        //不能用stream往List中添加
        //priorityQueue.stream().forEach(resList::add);
        while (priorityQueue.size()>0){
            resList.add(priorityQueue.poll());
        }
        Collections.reverse(resList);
        return resList;

    }

    public static void t1(PriorityQueue p){
        p.stream().forEach(System.out::println);
        System.out.println("------");
    }

    public static void t2(PriorityQueue p){
        while (p.size()>0){
            System.out.println(p.poll());
        }
    }
}

